#include "rclcpp/rclcpp.hpp"
#include "rms_pkg/msg/sensor_data.hpp"

class DataSubscriber : public rclcpp::Node {
public:
    DataSubscriber() : Node("sensor_hcd5014_subscriber") {
        subscription_ = create_subscription<rms_pkg::msg::SensorData>(
            "sensor_hcd5014_data", 10,
            [this](const rms_pkg::msg::SensorData::SharedPtr msg) {
                RCLCPP_DEBUG(get_logger(),
                            "\n接收到传感器数据:\n"
                            "robot_id: %s\n"
                            "空气温度: %.2f ℃\n"
                            "空气湿度: %.2f %%\n"
                            "光照强度: %.2f Lux\n"
                            "CO2浓度: %u ppm\n"
                            "土壤温度: %.2f ℃\n"
                            "土壤湿度: %.1f %%",
                            msg->robot_id.c_str(),
                            msg->air_temperature, msg->air_humidity, msg->light_intensity,
                            msg->co2_concentration, msg->soil_temperature, msg->soil_moisture);

                // 可在此添加更多处理逻辑（例如控制执行器、写入文件等）
            });
    }

private:
    rclcpp::Subscription<rms_pkg::msg::SensorData>::SharedPtr subscription_;
};

int main(int argc, char* argv[]) {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<DataSubscriber>());
    rclcpp::shutdown();
    return 0;
}